let rtcroom = require('../../utils/rtcroom.js');
const backgroundAudioManager = wx.getBackgroundAudioManager();
const app = getApp();

Page({
  data: {
    roomName: '', // 房间名称
    userName: '', // 用户名称
    roomList: [], // 房间列表
    tapTime: '', // 防止两次点击操作间隔太快
    timerID: null,
    inPage: true,  //是否在当前页面中
  },
  collectformid(e){
    let formid = e.detail.formId
    let openid = wx.getStorageSync('openid') || wx.getStorageSync('wsOpenID') || wx.getStorageSync('OPENID')
    app.util.request({
      url: 'pushService/api/formid',
      data: {
        formid,
        openid
      },
      method: 'GET'
    })
  },
  /**
   * [getRoomList 拉取房间列表]
   * @return {[type]} [description]
   */
 
  getRoomList: function() {
    let self = this;

    let {
      audio_url: audioUrl = '', audio_title: audioTitle = '',
    } = wx.getStorageSync('alarmBaseInfo');

    let {
      inPage,
    } = this.data;

    rtcroom.getRoomList({
      data: {
        index: 0,
        cnt: 20,
      },
      success: function(ret) {
        clearTimeout(self.data.timerID);
        self.setData({
          roomList: ret.rooms,
        });
        console.log('获取房间列表成功');
        console.log('========roomList', ret.rooms);

        const roomLists = ret.rooms;
        if (roomLists.length > 0) {
          roomLists.map(item => {
            if (item.pushers.length === 1 && audioUrl && inPage) {
              backgroundAudioManager.title = audioTitle || '系统提示音';
              backgroundAudioManager.src = audioUrl;
            }
          });
        }else{
          backgroundAudioManager.stop();
        }

        const timerID = setTimeout(() => {
          self.getRoomList();
        }, 2000);
        self.setData({
          timerID,
        });
      },
      fail: function(ret) {
        console.log(`roomlist getRoomList error`,ret);
      },
    });
  },

  /**
   * [goRoom 进入rtcroom页面]
   * @param  {[type]} e [description]
   * @return {[type]}   [description]
   */
  goRoom: function(e) {
    // 防止两次点击操作间隔太快
    let nowTime = new Date();
    if (nowTime - this.data.tapTime < 1000) {
      return;
    }
    if (e.currentTarget.dataset.num > 2) {
      wx.showModal({
        title: '提示',
        content: '房间人数已满',
        showCancel: false,
        complete: function() {},
      });
      return;
    }
    backgroundAudioManager.stop();
    const url = `../customer-service-room/customer-service-room?roomID=${e.currentTarget.dataset.roomid}&roomName=${e.currentTarget.dataset.roomname}&userName=${this.data.userName}`;
    wx.navigateTo({ url: url });
    this.setData({
      tapTime: nowTime,
      inPage: false,
    });
  },

  getBaseInfo(){
    let { recept_lobby: title = '' } = wx.getStorageSync('alarmBaseInfo');
    wx.setNavigationBarTitle({
      title,
    });
    this.setData({
      inPage: true,
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      userName: options.userName,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    wx.setKeepScreenOn({
      keepScreenOn: true,
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getBaseInfo();
    this.getRoomList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    backgroundAudioManager.stop();
    clearTimeout(this.data.timerID);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.getRoomList();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

});
